<template>
  <div>
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>查看商品</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-alert
        title="添加商品信息"
        type="info"
        center
        show-icon
        :closable="false"
      >
      </el-alert>

      <el-steps
        :space="200"
        align-center
        :active="activeIndex - 0"
        finish-status="success"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-position="top"
      >
        <el-tabs
          :tab-position="'left'"
          v-model="activeIndex"
          :before-leave="beforeTabLeave"
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="editForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="editForm.goods_price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input
                v-model="editForm.goods_weight"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input
                v-model="editForm.goods_number"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader
                v-model="editForm.goods_cat"
                :options="cateList"
                :props="cateListProps"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <el-form-item
              v-for="item in manyTableData"
              :label="item.attr_name"
              :key="item.attr_id"
            >
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox
                  v-for="(cn, i) in item.attr_vals"
                  :label="cn"
                  :key="i"
                  border
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
            <el-form-item
              v-for="item in onlyTableData"
              :key="item.attr_id"
              :label="item.attr_name"
            >
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <el-upload
              :action="uploadUrl"
              :on-preview="handlePreview"
              :on-success="uploadSuccessed"
              :file-list="fileList"
              list-type="picture"
              :headers="headerObj"
            >
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <quill-editor v-model="editForm.goods_introduce"></quill-editor>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>

    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="previewVisible" width="50%">
      <img :src="previewUrl" alt="" width="100%" />
    </el-dialog>
  </div>
</template>

<script>
// import _ from 'lodash'
export default {
  data() {
    return {
      activeIndex: '0',
      editForm: {
        goods_name: '',
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0,
        goods_cat: [],
        pics: [],
        goods_introduce: '',
        attrs: []
      },
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_cat: [
          { required: true, message: '请选择商品分类', trigger: 'blur' }
        ]
      },
      cateList: [],
      cateListProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      manyTableData: [],
      onlyTableData: [],
      // 图片上传
      uploadUrl: 'http://127.0.0.1:8888/api/private/v1/upload',
      fileList: [],
      headerObj: {
        Authorization: window.sessionStorage.getItem('token')
      },
      previewUrl: '',
      previewVisible: false
    }
  },
  activated() {},
  watch: {},
  created() {
    // console.log(this.$route.query)
    this.getInfoList()
    this.getCateList()
  },
  computed: {
    cateID() {
      if (this.editForm.goods_cat.length === 3) {
        return this.editForm.goods_cat[2]
      }
      return null
    }
  },
  mounted() {},
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类参数失败')
      }
      this.cateList = res.data
    },
    async getInfoList() {
      const { data: res } = await this.$http.get(
        `goods/${this.$route.query.goods_id}`
      )
      if (res.meta.status !== 200) return this.$message.error('获取信息失败')
      this.editForm.goods_name = res.data.goods_name
      this.editForm.goods_price = res.data.goods_price
      this.editForm.goods_weight = res.data.goods_weight
      this.editForm.goods_number = res.data.goods_number
      this.editForm.goods_introduce = res.data.goods_introduce
      this.editForm.goods_cat = res.data.goods_cat.split(',').map(i => i - 0)
      res.data.pics.forEach(item => {
        const picInfo = { name: item.goods_id, url: item.pics_big_url }
        this.fileList.push(picInfo)
      })
      res.data.attrs.forEach(item => {
        if (item.attr_sel === 'only') {
          this.onlyTableData.push(item)
        } else if (item.attr_sel === 'many') {
          item.attr_vals = item.attr_vals.split(' ')
          this.manyTableData.push(item)
        }
      })
      console.log(res.data)
    },
    handleChange() {
      if (this.editForm.goods_cat.length !== 3) {
        this.editForm.goods_cat = ''
      }
    },
    beforeTabLeave(activeName, oldActiveName) {
      if (oldActiveName === '0' && this.editForm.goods_cat.length !== 3) {
        this.$message.error('请选择商品分类')
        return false
      }
    },
    // 图片上传
    handlePreview(file) {
      this.previewUrl = file.url
      this.previewVisible = true
      console.log(file)
    },
    changePics(fileList) {
      this.editForm.pics = []
      fileList.forEach(item => {
        const picItem = { pic: item.response.data.tmp_path }
        this.editForm.pics.push(picItem)
      })
    },
    uploadSuccessed(response, uploadFile, fileList) {
      this.changePics(fileList)
      console.log(fileList)
    }
  }
}
</script>

<style lang="less" scoped>
.el-checkbox {
  margin: 0 10px 0 0 !important;
}
.addBtn {
  margin: 10px 0 0 0;
}
</style>
